package com.quotes.ui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import com.quotes.datamodell.DataChangedEventListener;
import com.quotes.datamodell.DataRepository;

public class SymbolsTable extends JPanel implements DataChangedEventListener
{
	private static final long serialVersionUID = 1L;
	private DataRepository repository = null;
	private JTable table = null;

	public SymbolsTable(DataRepository repository) {
		this.repository = repository;
		this.setLayout(new BorderLayout());
		repository.addDataChangedEventListener(this);
		
		reloadSymbolsIntoTable();
		JScrollPane scrollPane = new JScrollPane(table);

		add(scrollPane, BorderLayout.CENTER);
	}
	
	private void reloadSymbolsIntoTable() {
		SymbolsTableModel tableModel = new SymbolsTableModel(repository.getSymbols());
		table = new JTable(tableModel);
	}

	@Override
	public void dataChanged(DataRepository repository) {
		if (repository.isDataLoaded()) {
			reloadSymbolsIntoTable();
		}
	}
}
